﻿using BW.IBLL;
using BW.Model;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace BW.API.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class MomberController : ControllerBase
    {
        IBaseBLL bll;
        public MomberController(IBaseBLL bll)
        {
            this.bll = bll;
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<int> Add(MombetModel t)
        {
            try
            {
                return await bll.Add(t);
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 反填
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<MombetModel> Find(int id)
        {
            try
            {
                return await bll.Find(id);
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 显示
        /// </summary>
        /// <param name="pageindex"></param>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetShow(int pageindex = 1, int pagesize = 3)
        {
            int totalcount = 0;
            int totalpage = 0;
            var list = bll.GetShow(out totalcount, out totalpage, pageindex, pagesize);
            return Ok(new
            {
                totalcount = totalcount,
                totalpage = totalpage,
                list = list
            });
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<int> update(MombetModel t)
        {
            try
            {
                return await bll.update(t);
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="username"></param>
        /// <param name="pwd"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<dynamic> Login(string username, string pwd)
        {
            try
            {
                return await bll.Login(username, pwd);
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 上传图片
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        [HttpPost]
        public  IActionResult Imgs(IFormFile file)
        {
            string p = Directory.GetCurrentDirectory()+@"/wwwroot/Pic/"+file.FileName;
            using (FileStream fs = new FileStream(p, FileMode.Create))
            {
                file.CopyTo(fs);
                fs.Close(); 
            }
            return Ok("https://localhost:7192/Pic/"+file.FileName);
        }
    }
}
